---
overviewHeaders: [2, 3]
---

# lib.shims

- **Type:**

```ts
type Shims = {
  cjs?: {
    'import.meta.url'?: boolean;
  };
  esm?: {
    __filename?: boolean;
    __dirname?: boolean;
    require?: boolean;
  };
};
```

- **Default:**

```js
const defaultShims = {
  cjs: {
    'import.meta.url': true,
  },
  esm: {
    __filename: false,
    __dirname: false,
    require: false,
  },
};
```

Configure the [shims](https://developer.mozilla.org/en-US/docs/Glossary/Shim) for CommonJS and ESM output.

## shims.cjs

Set the fields to `true` to enable the corresponding shims for CommonJS output.

### shims.cjs['import.meta.url']

Whether to inject shims for the `import.meta.url` in CommonJS output.

- **Default:** `true`

Options:

- `true`: when [format](/config/lib/format) is `cjs`, the `import.meta.url` in source code will be replaced with the URL of the current module.

  For example, given the following source code:

  ```js
  import { readFileSync } from 'fs';
  const buffer = readFileSync(new URL('./data.proto', import.meta.url));
  ```

  the CJS output will be transformed to:

  ```js
  const { readFileSync } = require('fs');
  const buffer = readFileSync(
    new URL(
      './data.proto',
      /*#__PURE__*/ (function () {
        return typeof document === 'undefined'
          ? new (module.require('url'.replace('', '')).URL)(
              'file:' + __filename,
            ).href
          : (document.currentScript && document.currentScript.src) ||
              new URL('main.js', document.baseURI).href;
      })(),
    ),
  );
  ```

- `false`: the `import.meta.url` will be leave as is, which will cause a runtime error when running the output.

## shims.esm

Set the fields to `true` to enable the corresponding shims for ESM output.

### shims.esm.\_\_filename

Whether to inject shims for the global `__filename` of CommonJS in ESM output.

- **Default:** `false`

Options:

- `true`: when [format](/config/lib/format) is `esm`, the `__filename` in source code will be replaced with the filename of the current module.

  For example, given the following source code:

  ```js
  console.log(__filename);
  ```

  the ESM output will be transformed to:

  ```js
  import { fileURLToPath as __webpack_fileURLToPath__ } from 'url';
  import { dirname as __webpack_dirname__ } from 'path';
  var src_dirname = __webpack_dirname__(
    __webpack_fileURLToPath__(import.meta.url),
  );
  var src_filename = __webpack_fileURLToPath__(import.meta.url);
  console.log(src_filename);
  ```

- `false`: the `__filename` will be leave as is, which will cause a runtime error when running the output.

### shims.esm.\_\_dirname

Whether to inject shims for the global `__dirname` of CommonJS in ESM output.

- **Default:** `false`

Options:

- `true`: when [format](/config/lib/format) is `esm`, the `__dirname` in source code will be replaced with the directory name of the current module.

  For example, given the following source code:

  ```js
  console.log(__dirname);
  ```

  the ESM output will be transformed to:

  ```js
  import { fileURLToPath as __webpack_fileURLToPath__ } from 'url';
  import { dirname as __webpack_dirname__ } from 'path';
  var src_dirname = __webpack_dirname__(
    __webpack_fileURLToPath__(import.meta.url),
  );
  console.log(src_dirname);
  ```

- `false`: the `__dirname` will be leave as is, which will cause a runtime error when running the output.

### shims.esm.require

Whether to inject shims for the global `require` of CommonJS in ESM output.

- **Default:** `false`

Options:

- `true`: when [format](/config/lib/format) is `esm`, there will be a `require` that created by `createRequire` at the beginning of the output which can be accessed in source code like the global `require` like CommonJS.

  For example, given the following source code:

  ```js
  const someModule = require('./someModule');

  // dynamic require
  const dynamicRequiredModule = require(SOME_VALUE_IN_RUNTIME);
  // require.resolve
  const someModulePath = require.resolve('./someModule');
  // use require as a expression
  const lazyFn = (module, requireFn) => {};
  lazyFn('./other.js', require);
  ```

  the ESM output will be transformed to:

  ```js
  import __rslib_shim_module__ from 'module';
  const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(
    import.meta.url,
  );
  // dynamic require
  require(SOME_VALUE_IN_RUNTIME);
  // require.resolve
  require.resolve('./someModule');
  // use require as a expression
  const lazyFn = (module, requireFn) => {};
  lazyFn('./other.js', require);
  ```

- `false`: the `require` will be leave as is, which will cause a runtime error when running the output.
